package com.yanjin.dao;

import java.io.IOException;

import java.sql.Connection;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.Request;

import com.jdbc.tool.*;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;
import com.yanjin.shuji.User;


public class UserDao extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		  String userid=request.getParameter("userId");
    	  String username=request.getParameter("username");
    	  String userpass=request.getParameter("userpass");
    	  String sex=request.getParameter("sex");
    	  String date=request.getParameter("sr");
          SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
          Date date1=null;
          try {
				date1=sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	  
    	  String userCarid=request.getParameter("userCarid");
    	  String email=request.getParameter("email");
    	  String pone=request.getParameter("pone");
    	  String addres=request.getParameter("addres");
    	  String jibie=request.getParameter("jibie");
    	  String beizhu=request.getParameter("beizhu");
    	  
    	  //System.out.println(userid+username+userpass+sex+date+userCarid+email+pone+addres+jibie+beizhu);
    	  Connection conn=null;
    	  Statement stm=null;
    	  
    	  conn=DBLianjie.getconn();
    	  try {
			stm = conn.createStatement();
			int x=stm.executeUpdate("update emai_user set e_user_id='"+userid+"',E_USER_NAME='"+username+"',E_PASSWORD='"+userpass+"',E_SEX='"+sex+"',E_BRITHDAY=to_date('"+date+"','yyyy-MM-dd'),E_IDENTITY_CODE='"+userCarid+"',E_EMIAL='"+email+"',E_MOBILE='"+pone+"',E_ADDRESS='"+addres+"',E_STATUS='"+jibie+"',E_LJ='"+beizhu+"' where e_user_id='"+userid+"'");
		  if(x!=0){
    		  response.sendRedirect("Geren.jsp?userid='"+userid+"'");
    	  }else{
    		  response.sendRedirect("shiban.jsp");
    	  }
    	  } catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(stm!=null){
				try {
					stm.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
      }
    public boolean deleterUser(String userId){
  	  boolean x=false;
  	  Connection conn=null;
  	  PreparedStatement Pstm=null;
  	  String sql="delete from emai_user where e_user_id=?";
  	  conn=DBLianjie.getconn();
  	  try {
			Pstm=conn.prepareStatement(sql);
			Pstm.setString(1, userId);
			Pstm.execute();
			x=true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
  	  return x;
    }
    
    
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String userid=request.getParameter("userId");
		String username=request.getParameter("username");
		String passWord=request.getParameter("userpass");
		String sex=request.getParameter("sex");
		String birthyear=request.getParameter("birthyear");
		String birthmonth=request.getParameter("birthmonth");
		String birthday=request.getParameter("birthday");
		String brithDay=birthyear+"-"+birthmonth+"-"+birthday;	
		brithDay="19"+brithDay;
  	  String userCarid=request.getParameter("userCarid");
	  String email=request.getParameter("email");
	  String pone=request.getParameter("pone");
	  String addres=request.getParameter("addres");
	  String jibie=request.getParameter("jibie");
	  String beizhu=request.getParameter("beizhu");

		
		Connection conn=null;
		Statement stm=null;
		String sql="update emai_user set E_USER_ID='"+userid+"',E_USER_NAME='"+username+"',E_PASSWORD='"+passWord+"',E_SEX='"+sex+"',E_BRITHDAY=to_date('"+brithDay+"','yyyy-MM-dd'),E_IDENTITY_CODE='"+userCarid+"',E_EMIAL='"+email+"',E_MOBILE='"+pone+"',E_ADDRESS='"+addres+"',E_STATUS='"+jibie+"',E_LJ='"+beizhu+"' where e_user_id='"+userid+"'";
        conn=DBLianjie.getconn();
       try {
		stm=conn.createStatement();
		boolean x=stm.execute(sql);

		if(!x){
			response.sendRedirect("SelectAllUser.jsp");
		}else{
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
	}

	
	
	
    public ArrayList<User> findUser(String id,String mobil){

  	  ArrayList<User> list=new ArrayList<User>();
  	  Connection conn=null;
  	  Statement stm=null;
  	  ResultSet rs=null;
  	  conn=DBLianjie.getconn();
  	  String sql="select * from emai_user";
  	  if(id!=null&&!"".equals(id.trim())){
  		  sql=sql+" where e_user_id like '%"+id+"%'";
  	  }
  	  if(mobil!=null&&!"".equals(mobil.trim())){
  		  if(sql.indexOf("where")==-1){
  			  sql=sql+"where e_mobile like '%"+mobil+"%'";
  		  }else{
  			  sql=sql+"and e_mobile like '%"+mobil+"%'";
  		  }
  	  }
  	  try {
  		  stm = conn.createStatement();
			  rs=stm.executeQuery(sql);
			while(rs.next()){
				User u= new User();
				u.setEmail(rs.getString("e_emial"));
				u.setMobile(rs.getString("e_mobile"));
				u.setSex(rs.getString("e_sex"));
				u.setUserId(rs.getString("e_user_id"));
				u.setUserName(rs.getString("e_user_name"));
				u.setAddress(rs.getString("E_ADDRESS"));
				u.setDrithday(new java.util.Date(rs.getDate("e_brithday").getTime()));
				u.setPassWord(rs.getString("E_PASSWORD"));
				u.setBeizhu(rs.getString("E_LJ"));
				u.setDengji(rs.getString("E_STATUS"));
				u.setIdCard(rs.getString("E_IDENTITY_CODE"));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			
		}
		return list;
    }
}
